﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using AutoMapper;
namespace Data
{
    public class HoaDonRepository
    {

        public static List<HoaDonDTO> GetHoaDonList()
        {
            
            NhaSachEntities context = new NhaSachEntities();
            var query = from hoadons in context.HoaDonList
                        select new HoaDonDTO
                        {
                            MaHoaDon = hoadons.MaHoaDon,
                            NgayLap =(DateTime) hoadons.NgayLapHoaDon,
                            MaKhachHang  = hoadons.KhachHang.MaKhachHang
                        };

            return query.ToList();
        }
        public static HoaDonDTO GetHoaDon(int iMaHoaDon)
        {
            NhaSachEntities context = new NhaSachEntities();
            var query = context.HoaDonList.Single(hd => hd.MaHoaDon == iMaHoaDon);
            HoaDonDTO kq = new HoaDonDTO();
            kq.MaHoaDon = query.MaHoaDon;
            kq.MaKhachHang = query.KhachHang.MaKhachHang;
            kq.NgayLap = (DateTime) query.NgayLapHoaDon;
            kq.TinhTrang = 0;
            
            return kq;
        }

        public static void Insert(HoaDonDTO hoaDon)
        {
            NhaSachEntities context = new NhaSachEntities();
            HoaDon hoaDonEF = new HoaDon();
            Mapper.CreateMap<HoaDonDTO, HoaDon>();
            hoaDonEF = Mapper.Map<HoaDonDTO, HoaDon>(hoaDon);

            context.AddToHoaDonList(hoaDonEF);
            context.SaveChanges();

        }

    }
}
